﻿using KouDai.Core.Helper;
using Modern.MvvM;
using System.Windows.Input;

namespace KouDai.Core.Service.Navigation
{
    public class NavigateViewModel : SimpleViewModel
    {
        #region Fields
        private readonly INavigateService _navigateService;
        private ICommand _goBackCommand;
        private ICommand _goHomeCommand;
        private object _data;
        #endregion
      
        #region Properties
        public NavigateViewModel(INavigateService navigateService)
        {
            _navigateService = navigateService;
        } 

        public object Data
        {
            get { return _data; }
            set
            {
                _data = value;
                OnDataSet();
            }
        }
        #endregion

        #region Command
        public ICommand GoBackCommand
        {
            get { return _goBackCommand ?? (_goBackCommand = new NavigateCommand(GoBackExecute, _navigateService)); }
        }

        public ICommand GoHomeCommand
        {
            get { return _goHomeCommand ?? (_goHomeCommand = new NavigateCommand(GoHomeExecute, _navigateService)); }
        }
        #endregion

        #region Method
        private void GoBackExecute()
        {
            _navigateService.GoBack();
        }

        private void GoHomeExecute()
        {
            while (_navigateService.CanGoBack)
            {
                _navigateService.GoBack();
            }
        }

        protected virtual void OnDataSet()
        {

        }
        #endregion
    }
}
